package Ep06_BinaryTree.BFS.T5_0637;

import Ep06_BinaryTree.TreeNode;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class Mine {
    public List<Double> averageOfLevels (TreeNode root) {
        List<Double> res = new LinkedList<>();
        if (root == null) return res;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        while(!deque.isEmpty()){
            int size = deque.size();
            int len = deque.size();
            double count = 0.0;
            while (size > 0){
                TreeNode tempNode = deque.poll();
                // 只剩最后一个节点的时候进行数据记录即可
                count += tempNode.val;
                if (tempNode.left != null){
                    deque.offer(tempNode.left);
                }
                if (tempNode.right != null){
                    deque.offer(tempNode.right);
                }
                size--;
            }
            res.add(count / len);
        }
        return res;
    }
}
